package com.example.sqlitelogin;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sqlitelogin.tools.DataChange;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager2 viewPager2;
    private LinearLayout llChat,llContacts,llFind,llProfile;
    private ImageView ivChat,ivContacts,ivFind,ivProfile,ivCurrent;
    private LinearLayout add;
    private String name1,name2,name3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //状态栏中的文字颜色和图标颜色，需要android系统6.0以上，而且目前只有一种可以修改（一种是深色，一种是浅色即白色）
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //修改为深色，因为我们把状态栏的背景色修改为主题色白色，默认的文字及图标颜色为白色，导致看不到了。
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setContentView(R.layout.activity_main);
        initPager();
        initTabView();
        add=findViewById(R.id.id_tab_add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent6=new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent6);
            }
        });
    }

    private void initTabView() {
        llChat=findViewById(R.id.id_tab_znjj1);
        llChat.setOnClickListener(this);
        llContacts=findViewById(R.id.id_tab_znjj2);
        llContacts.setOnClickListener(this);
        llFind=findViewById(R.id.id_tab_znjj3);
        llFind.setOnClickListener(this);
        llProfile=findViewById(R.id.id_tab_znjj4);
        llProfile.setOnClickListener(this);

        ivChat=findViewById(R.id.tab_iv_znjj1);
        ivContacts=findViewById(R.id.tab_iv_znjj2);
        ivFind=findViewById(R.id.tab_iv_znjj3);
        ivProfile=findViewById(R.id.tab_iv_znjj4);



        ivChat.setSelected(true);
        ivCurrent=ivChat;

    }

    private void initPager() {
        viewPager2=findViewById(R.id.viewpager1);
        Intent intent = getIntent();

         name1 = intent.getStringExtra("loact");
         name2 = intent.getStringExtra("temp12");
         name3 = intent.getStringExtra("temp34");
        String name4 = intent.getStringExtra("name");
        String direct=intent.getStringExtra("direct");
        String power=intent.getStringExtra("power");
        String aqi=intent.getStringExtra("aqi");


        String date2=intent.getStringExtra("date2");
        String temperature2=intent.getStringExtra("temperature2");
        String weather2=intent.getStringExtra("weather2");
        String direct2=intent.getStringExtra("direct2");
        String date3=intent.getStringExtra("date3");
        String temperature3=intent.getStringExtra("temperature3");
        String weather3=intent.getStringExtra("weather3");
        String direct3=intent.getStringExtra("direct3");



        Log.e("leo",name1+" "+name2+" "+name3+" "+name4+" "+direct+" "+power+" "+aqi);
        ArrayList<Fragment> fragments=new ArrayList<>();
        fragments.add(BlankFragment1.newInstance(name1,name2,name3,direct,power,aqi,date2,temperature2,weather2,direct2,date3,temperature3,weather3,direct3));

        fragments.add(BlankFragment3.newInstance("sss"));
        fragments.add(BlankFragment2.newInstance("微ss信聊天"));
        fragments.add(BlankFragment4.newInstance(name4));
        MyfragmentPagerAdapter pagerAdapter=new MyfragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager2.setAdapter(pagerAdapter);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                chagerTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void chagerTab(int position) {
        ivCurrent.setSelected(false);
        switch (position){
            case R.id.id_tab_znjj1:
                viewPager2.setCurrentItem(0);
            case 0:
                ivChat.setSelected(true);
                ivCurrent=ivChat;
                break;
            case R.id.id_tab_znjj2:
                viewPager2.setCurrentItem(1);
            case 1:
                ivContacts.setSelected(true);
                ivCurrent=ivContacts;
                break;
            case R.id.id_tab_znjj3:
                viewPager2.setCurrentItem(2);
            case 2:
                ivFind.setSelected(true);
                ivCurrent=ivFind;
                break;
            case R.id.id_tab_znjj4:
                viewPager2.setCurrentItem(3);
            case 3:
                ivProfile.setSelected(true);
                ivCurrent=ivProfile;
                break;
        }
    }

    @Override
    public void onClick(View view) {
        chagerTab(view.getId());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("leo","onRestart");

//        if (DataChange.Cty!=null){
//            name2=DataChange.Te;
//            name3=DataChange.dalt;
//            name1=DataChange.Cty+"市";
//        }
//        TextView locat=findViewById(R.id.location);
//
//        locat.setText(name1);
//
//        TextView temp_0=findViewById(R.id.temp_te);
//        temp_0.setText(name2+"℃");
//
//        TextView daylight_0=findViewById(R.id.daylight);
//        daylight_0.setText("今日 "+name3);

    }



    @Override
    protected void onResume() {
        super.onResume();
        Log.e("leo","onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("leo","onStart");
    }




}
